import  tushare  as  ts
import  pandas as pd
import get_stock_data_path as sp

def Compare(df1:pd.DataFrame,df2:pd.DataFrame):
    if(df1.shape[0] != df2.shape[0]):
      return False
    if(df1.shape[1] != df2.shape[1]):
      return False
    for i in range(df1.shape[0]):
        for j in df1.columns:
            if(df1.at[i,j] != df2.at[i,j]):
                if(df1.at[i,j] is None and df2.at[i,j]!=df2.at[i,j]):
                    continue
                return False
    return True



def get_stock_basic():
  stock_path = sp.get_stock_data_path()
  print("tushare version:")
  print(ts.__version__)
  ts.set_token("e5674ba266792c5fa237973829828dbcde2e71aa72b3eaa721b51352")
  pro = ts.pro_api()
  data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
  #print(data)
  stock_basic_csv = stock_path + sp.get_separator() + "stock_basic.csv"
  old_data=pd.read_csv(stock_basic_csv, dtype={"symbol":str,"list_date":str})
  compare = Compare(data, old_data)
  if(compare==False):
      print("not match")
      data.to_csv(stock_basic_csv,index = False)



if __name__ == '__main__':
    pd.set_option('display.unicode.ambiguous_as_wide', True)
    pd.set_option('display.unicode.east_asian_width', True)
    pd.set_option('display.max_rows', None)
    get_stock_basic()